const source = require("../../public/source");

module.exports = {
    //str: "50,1;30,2;10,3;10,4-5-6"
    toWeightArr(str) {
        let arr = str.split(";").map(item => {
            let arr = item.split(",");
            arr[0] = parseInt(arr[0])
            arr[1] = arr[1].split("-").map(ite => {
                return parseInt(ite)
            });
            return arr;
        })
        return arr;
    },
    //范围字符串转成sql语句需要的数组格式
    //str: 数字1,2... 关系式<5，>10  区间数组"（1,9]"
    toSqlWhereArr(key,str) {
        let arrs = [];
        str=str.toString();
        if (/^[0-9]+$/.test(str)) {
            arrs.push([key, str])
        } else if (/^<=|>=|<|>|=[0-9]+$/.test(str)) {
            let ope = str.match(/<=|>=|<|>|=/)[0], num = str.split(ope)[1]
            arrs.push([key, num, ope])
        } else if (source.regexp.areaStr.test(str)) {
            let { leftBracket, rightBracket, min, max } = this.resolveAreaStr(str);
            let leftOpe = leftBracket === "(" ? ">" : ">=", rightOpe = rightBracket === ")" ? "<" : "<=";
            arrs.push([key, min, leftOpe], [key, max, rightOpe])
        }else{
            arrs.push([key, str]);
        }
        return arrs;
    },
    //解析区间数组，返回{leftBracket,rightBracket,min,max}
    resolveAreaStr(areaStr) {
        areaStr = areaStr.replace(/\s/g, "");
        let areaArr = areaStr.replace(/[\[\]\(\)]/g, "").split(",");
        let min = parseInt(areaArr[0]);
        let max = parseInt(areaArr[1]);
        let leftBracket = areaStr.charAt(0);
        let rightBracket = areaStr.charAt(areaStr.length - 1);
        return { leftBracket, rightBracket, min, max }
    }
    //将字符串分解成关键词
    // type: goods,user
    //返回值：obj={brand:[],color:[],battery:[]...}
    // keyWords(str, type) {
    //     let obj = {};   
    //     let pool = source.searchWords[type];
    //     let isEnd = false;
    //     for (let key in pool) {
    //         for (let i = 0; i < pool[key].length; i++) {
    //             let item = pool[key][i];
    //             if (str.includes(item)) {
    //                 obj[key] ? obj[key].push(item) : obj[key] = new Array(item);
    //                 str = str.replace(item, " ");
    //                 if (str.replace(/ /g, "").length <= 1) {
    //                     isEnd = true;
    //                     break;
    //                 }
    //             }
    //         }
    //         if (isEnd) {
    //             break;
    //         }
    //     }
    //     return obj;
    // }
}